package com.hera.base.web.vaadin.mantenimiento;

import static com.hera.base.util.InputData.getIntegerData;
import static com.hera.base.util.InputData.getObjectData;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.hera.base.util.Constantes;
import com.hera.base.util.FiltroDTO;
import com.hera.base.util.TableCustom;
import com.hera.base.web.BaseUI;
import com.hera.base.web.MantenimientoMaestroDetalle;
import com.hera.process.config.domain.ParameterDetail;
import com.hera.process.config.rest.delegate.ConfigDelegate;
import com.hera.process.config.rest.delegate.FormDelegate;
import com.hera.process.form.domain.Dynaform;
import com.hera.process.form.domain.DynaformStage;
import com.hera.process.form.domain.DynaformStagePK;
import com.hera.process.workflow.domain.Process;
import com.hera.process.workflow.domain.Stage;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class DynaformStageUI extends BaseUI implements MantenimientoMaestroDetalle,ClickListener {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	/**
	 * 
	 */
	private static final long serialVersionUID = -8335126979086915804L;
	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private VerticalLayout verticalLayout_1;
	@AutoGenerated
	private HorizontalLayout lytMaestro;
	@AutoGenerated
	private VerticalLayout pnlFormulario;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_5;
	@AutoGenerated
	private Button btnEliminar;
	@AutoGenerated
	private Button btnNuevo;
	@AutoGenerated
	private TextField txtOrden;
	@AutoGenerated
	private ComboBox cmbModo;
	@AutoGenerated
	private ComboBox cmbFormulario;
	@AutoGenerated
	private Label lbltitulo;
	@AutoGenerated
	private HorizontalLayout lytTablaMaestra;
	@AutoGenerated
	private TableCustom tblfMaestro;
	private Stage stage;
	private Process process;
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public DynaformStageUI() {
		buildMainLayout();
		setCompositionRoot(mainLayout);
	}
	
	public DynaformStageUI(Stage stage, Process process) {
		buildMainLayout();
		setCompositionRoot(mainLayout);
		this.stage = stage;
		this.process = process;
	}
	
	public void setStage(Stage stage){
		this.stage = stage;
	}

	private static final Logger log = Logger.getLogger(DynaformStageUI.class);
	
	@Autowired
	ConfigDelegate configDelegate;
	
	@Autowired
	FormDelegate formDelegate;
	
	boolean esNuevo;
	boolean esNuevoDetalle;
	DynaformStage dynaformStage;
	
	
	@Override
	public void buttonClick(ClickEvent event) {
		if(event.getButton().equals(btnEliminar)){
			eliminarMaestro();
		}else if(event.getButton().equals(btnNuevo)){
			guardarMaestro();
		}
		
	}

	@Override
	public void cargarFormulario() {
		
		txtOrden.setNullRepresentation("");
	
		log.info("INCIO +++++++++");
		esNuevo=true;
		dynaformStage=null;

		btnEliminar.setVisible(false);
		btnEliminar.addClickListener(this);
		
		
		btnNuevo.setVisible(false);
		btnNuevo.addClickListener(this);
		btnNuevo.setCaption("Nuevo");
		
		FiltroDTO filtroDTO2=new FiltroDTO();
		filtroDTO2.addParametro("idProcess", process.getIdProcess());
		List<Dynaform> dynform1 = formDelegate.findAllDynaforms(filtroDTO2);
		//filtroDTO.addParametro("idProcess", processCurrrent.getIdProcess());
		BeanItemContainer<Dynaform> dynform2 = new BeanItemContainer<Dynaform>(Dynaform.class);
		dynform2.addAll(dynform1);
		cmbFormulario.setContainerDataSource(dynform2);
		cmbFormulario.setItemCaptionPropertyId("name");
		cmbFormulario.setInputPrompt("Formulario");
		
		BeanItemContainer<ParameterDetail> modopres = new BeanItemContainer<ParameterDetail>(ParameterDetail.class);
		ParameterDetail listsDetail = new ParameterDetail();
		listsDetail.setIdParameterDetail(Constantes.ModoFormulario.ID);
		modopres.addAll(configDelegate.findDetailList(listsDetail));
		cmbModo.setContainerDataSource(modopres);
		cmbModo.setItemCaptionPropertyId("name");
		cmbModo.setInputPrompt("Modo Presentación");
		
		
		//tblfMaestro.setTitulo(" Lista Formularios del Stage " + stage.getName());
		tblfMaestro.setHeight("150px");
		
		tblfMaestro.addColumn("dynaform", "Fomulario", 150, true, String.class);
		tblfMaestro.addColumn("secuence", "Orden", 50, true, String.class);
		//tblfMaestro.addColumn("schema", "Modo", 250, true, String.class);
		
		tblfMaestro.construir();
		tblfMaestro.addTextChangeListener(new TextChangeListener() {	
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void textChange(TextChangeEvent event) {
				buscarMaestro(event);
				
			}
		});
		
		tblfMaestro.addValueChangeListener(new ValueChangeListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				dynaformStage=(DynaformStage)tblfMaestro.getSelectedItem(event);
				esNuevo=(dynaformStage==null);
				tblfMaestro.limpiarFiltro();
				if(esNuevo){
					limpiarMaestro();
					cmbFormulario.setEnabled(true);
					btnEliminar.setVisible(false);
				}else{
					btnEliminar.setVisible(true);
					btnNuevo.setVisible(true);
					btnNuevo.setCaption("Modificar");
					cmbFormulario.setValue(dynaformStage.getDynaform());
					cmbFormulario.setEnabled(false);
					cmbModo.setValue(dynaformStage.getMode());
					txtOrden.setValue(""+dynaformStage.getSecuence());
				}				
				log.info("Fin Cargar Formulario DynamicForm stage ");
				
			}
		});		
		
	}

	@Override
	public void buscarMaestro() {
		buscarMaestro(null);
		
	}

	@Override
	public void buscarMaestro(TextChangeEvent event) {
		log.info("inicio Buscar DataSource");
		//FiltroDTO filtroDTO = tblfMaestro.getFiltroDTO(event);
		List<DynaformStage> lstDynaformStage = formDelegate.findAllDynaformStagesByStage(stage);
		tblfMaestro.setLstData(lstDynaformStage);
		limpiarMaestro();
		log.info("fin FindDatasource");
		
		
	}

	@Override
	public void limpiarMaestro() {
		log.info("inicio limpiarMaestro");
		btnNuevo.setVisible(true);
		btnNuevo.setCaption("Nuevo");
		cmbFormulario.setValue(null);
		cmbModo.setValue(null);
		txtOrden.setValue("");			
		log.info("fin limpiarMaestro");
		
	}

	@Override
	public void eliminarMaestro() {
		
		log.info("inicio eliminarMaestro");
		formDelegate.deleteDynaformStage(dynaformStage);
		buscarMaestro();
		limpiarMaestro();
		log.info("fin eliminarMaestro");	
		
	}

	@Override
	public void guardarMaestro() {
		
		log.info("inicio guardarMaestro");
		if(esNuevo){
			dynaformStage=new DynaformStage();
			obtenerDatosFormulario(dynaformStage);
			dynaformStage.setStage(stage);
			
			DynaformStagePK dynaformStagePK = new DynaformStagePK();
			dynaformStagePK.setIdDynaform(dynaformStage.getDynaform().getIdDynaform());
			dynaformStagePK.setIdStage(dynaformStage.getStage().getIdStage());
			dynaformStage.setDynaformStagePK(dynaformStagePK);
			
			
			formDelegate.createDynaformStage(dynaformStage);
		}else{
			obtenerDatosFormulario(dynaformStage);
			formDelegate.updateDynaformStage(dynaformStage);
		}
		limpiarMaestro();
		buscarMaestro();
		log.info("fin guardarMaestro");
	}

	@Override
	public void buscarDetalle() {
		
		
	}

	@Override
	public void buscarDetalle(TextChangeEvent event) {
		
		
	}

	@Override
	public void limpiarDetalle() {
		
		
	}

	@Override
	public void eliminarDetalle() {
		
		
	}

	@Override
	public void guardarDetalle() {
		
		
	}

	@Override
	protected void postConstruct() {
		cargarFormulario();
		buscarMaestro();
		
	}

private void obtenerDatosFormulario(DynaformStage dynaformStage) {		
	dynaformStage.setDynaform(getObjectData(cmbFormulario, Dynaform.class, "Formulario"));
	dynaformStage.setMode(getObjectData(cmbModo, ParameterDetail.class, "Modo Presentación"));
	dynaformStage.setSecuence(getIntegerData(txtOrden, "Orden"));
	
}

@AutoGenerated
private VerticalLayout buildMainLayout() {
	// common part: create layout
	mainLayout = new VerticalLayout();
	mainLayout.setImmediate(false);
	mainLayout.setWidth("-1px");
	mainLayout.setHeight("-1px");
	mainLayout.setMargin(false);
	
	// top-level component properties
	setWidth("-1px");
	setHeight("-1px");
	
	// verticalLayout_1
	verticalLayout_1 = buildVerticalLayout_1();
	mainLayout.addComponent(verticalLayout_1);
	
	return mainLayout;
}

@AutoGenerated
private VerticalLayout buildVerticalLayout_1() {
	// common part: create layout
	verticalLayout_1 = new VerticalLayout();
	verticalLayout_1.setImmediate(false);
	verticalLayout_1.setWidth("-1px");
	verticalLayout_1.setHeight("-1px");
	verticalLayout_1.setMargin(false);
	verticalLayout_1.setSpacing(true);
	
	// lytMaestro
	lytMaestro = buildLytMaestro();
	verticalLayout_1.addComponent(lytMaestro);
	
	return verticalLayout_1;
}

@AutoGenerated
private HorizontalLayout buildLytMaestro() {
	// common part: create layout
	lytMaestro = new HorizontalLayout();
	lytMaestro.setImmediate(false);
	lytMaestro.setWidth("-1px");
	lytMaestro.setHeight("-1px");
	lytMaestro.setMargin(false);
	lytMaestro.setSpacing(true);
	
	// lytTablaMaestra
	lytTablaMaestra = buildLytTablaMaestra();
	lytMaestro.addComponent(lytTablaMaestra);
	
	// pnlFormulario
	pnlFormulario = buildPnlFormulario();
	lytMaestro.addComponent(pnlFormulario);
	
	return lytMaestro;
}

@AutoGenerated
private HorizontalLayout buildLytTablaMaestra() {
	// common part: create layout
	lytTablaMaestra = new HorizontalLayout();
	lytTablaMaestra.setImmediate(false);
	lytTablaMaestra.setWidth("270px");
	lytTablaMaestra.setHeight("-1px");
	lytTablaMaestra.setMargin(false);
	
	// tblfMaestro
	tblfMaestro = new TableCustom();
	tblfMaestro.setImmediate(false);
	tblfMaestro.setWidth("-1px");
	tblfMaestro.setHeight("-1px");
	lytTablaMaestra.addComponent(tblfMaestro);
	
	return lytTablaMaestra;
}

@AutoGenerated
private VerticalLayout buildPnlFormulario() {
	// common part: create layout
	pnlFormulario = new VerticalLayout();
	pnlFormulario.setImmediate(false);
	pnlFormulario.setWidth("-1px");
	pnlFormulario.setHeight("-1px");
	pnlFormulario.setMargin(false);
	pnlFormulario.setSpacing(true);
	
	// lbltitulo
	lbltitulo = new Label();
	lbltitulo.setStyleName("titulo");
	lbltitulo.setImmediate(false);
	lbltitulo.setWidth("-1px");
	lbltitulo.setHeight("-1px");
	lbltitulo.setValue("Formularios");
	pnlFormulario.addComponent(lbltitulo);
	pnlFormulario.setComponentAlignment(lbltitulo, new Alignment(20));
	
	// cmbFormulario
	cmbFormulario = new ComboBox();
	cmbFormulario.setStyleName("fieldRequired");
	cmbFormulario.setImmediate(false);
	cmbFormulario.setDescription("Formulario");
	cmbFormulario.setWidth("-1px");
	cmbFormulario.setHeight("-1px");
	pnlFormulario.addComponent(cmbFormulario);
	
	// cmbModo
	cmbModo = new ComboBox();
	cmbModo.setStyleName("fieldRequired");
	cmbModo.setImmediate(false);
	cmbModo.setDescription("Modo");
	cmbModo.setWidth("-1px");
	cmbModo.setHeight("-1px");
	pnlFormulario.addComponent(cmbModo);
	
	// txtOrden
	txtOrden = new TextField();
	txtOrden.setStyleName("fieldRequired");
	txtOrden.setImmediate(false);
	txtOrden.setDescription("Orden");
	txtOrden.setWidth("-1px");
	txtOrden.setHeight("-1px");
	txtOrden.setInputPrompt("Orden");
	pnlFormulario.addComponent(txtOrden);
	
	// horizontalLayout_5
	horizontalLayout_5 = buildHorizontalLayout_5();
	pnlFormulario.addComponent(horizontalLayout_5);
	
	return pnlFormulario;
}

@AutoGenerated
private HorizontalLayout buildHorizontalLayout_5() {
	// common part: create layout
	horizontalLayout_5 = new HorizontalLayout();
	horizontalLayout_5.setImmediate(false);
	horizontalLayout_5.setWidth("-1px");
	horizontalLayout_5.setHeight("-1px");
	horizontalLayout_5.setMargin(false);
	horizontalLayout_5.setSpacing(true);
	
	// btnNuevo
	btnNuevo = new Button();
	btnNuevo.setCaption("Nuevo");
	btnNuevo.setImmediate(true);
	btnNuevo.setWidth("-1px");
	btnNuevo.setHeight("-1px");
	horizontalLayout_5.addComponent(btnNuevo);
	
	// btnEliminar
	btnEliminar = new Button();
	btnEliminar.setCaption("Eliminar");
	btnEliminar.setImmediate(true);
	btnEliminar.setWidth("-1px");
	btnEliminar.setHeight("-1px");
	horizontalLayout_5.addComponent(btnEliminar);
	
	return horizontalLayout_5;
}
	
	
}
